package com.woogiemobile;

import com.woogiemobile.async.DataToServer;
import com.woogiemobile.pojo.Call;
import com.woogiemobile.util.Util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class IncomingCallReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(final Context context, Intent intent) {
		TelephonyManager telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		CustomPhoneStateListener custom = new CustomPhoneStateListener();
		
		telManager.listen(custom, PhoneStateListener.LISTEN_CALL_STATE);
		
		Bundle bundle = intent.getExtras();
		String number = bundle.getString("incoming_number");
		Log.v("Custom Listener", "phone Numer: "+number);
	}

	class CustomPhoneStateListener extends PhoneStateListener{
		public void onCallStateChanged(int state, String incomingNumber) {
			Log.v("Phone Listener", incomingNumber);
			
			switch(state){
			case TelephonyManager.CALL_STATE_RINGING:
				Log.d("Phone Lister", "RINGING");
				Call call = new Call(System.currentTimeMillis(), incomingNumber, Util.getContactId(incomingNumber));
				new DataToServer(call.toString(), false).execute();
				break;
			}
		}
	}
}
